package com.openbravo.pos.panels.link;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.table.model.RowNumTableModelWapper;

import cn.ekuma.data.dao.bean.IKeyed;
import cn.ekuma.data.ui.swing.AbstractDTOTableModel;
import cn.ekuma.epos.datalogic.I_DataLogicSales;

import com.openbravo.data.user.EditorCreator;
import com.openbravo.pos.panels.event.BaseBeanChangedListener;

public abstract class AbstractBaseLinkTablePanel<T,K,I> extends JPanel implements BaseBeanChangedListener<K,I>,EditorCreator{

	protected AbstractDTOTableModel<T> tableModel;
	protected I baseBeanID;
	protected I_DataLogicSales dlSales;
	private javax.swing.JScrollPane jScrollPane1;
	protected org.jdesktop.swingx.JXTable jXTable1;
	protected JPanel panel;

	public AbstractBaseLinkTablePanel(I_DataLogicSales dlSales,final AbstractDTOTableModel<T> tableModel){
	initComponents();
    this.dlSales=dlSales;
    this.tableModel=tableModel;
    jXTable1.setModel(new RowNumTableModelWapper(tableModel));
    jXTable1.setAutoResizeMode(JXTable.AUTO_RESIZE_OFF);
    panel = new JPanel();
    add(panel, BorderLayout.NORTH);
    panel.setLayout(new BorderLayout(0, 0));
    JPanel toolbar=getToolBar();
    if(toolbar!=null)
    	panel.add(toolbar,java.awt.BorderLayout.NORTH);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	protected void initComponents() {
	    jScrollPane1 = new javax.swing.JScrollPane();
	    jXTable1 = new org.jdesktop.swingx.JXTable();
	    setLayout(new java.awt.BorderLayout());
	    jScrollPane1.setViewportView(jXTable1);
	
	    add(jScrollPane1, java.awt.BorderLayout.CENTER);
	}// </editor-fold>//GEN-END:initComponents

	protected boolean isBaseBeanIDChanged(I inProdcutID) {
	    if(inProdcutID==null){
	        if(baseBeanID!=null)
	            return true;
	    }else if(baseBeanID==null)
	            return true;
	      else if(!baseBeanID.equals(inProdcutID))
	            return true;
	    return false;
	}

	public JPanel getToolBar(){
		return null;
	}
	
	public void baseBeanChanged(IKeyed<I> productID,K o) {
		if(productID==null){
	        tableModel.resert(null);
    	    return;
    	}
    	if(!isBaseBeanIDChanged(productID.getKey()))
	         return;
	    this.baseBeanID=productID.getKey();
	    baseBeanChanged(o);
    }
	
	public abstract void baseBeanChanged(K o);
	
}
